package app.fugema.shop.main.login.view;

import android.os.Bundle;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.base.common.utils.DensityUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.utils.span.SpanUtils;
import com.base.common.view.base.BaseDialogFragment;
import com.base.common.viewmodel.BaseViewModel;
import app.fugema.shop.R;
import app.fugema.shop.databinding.UserAgreementDialogFragmentBinding;

public class UserAgreementDialogFragment extends BaseDialogFragment<UserAgreementDialogFragmentBinding, BaseViewModel> {

    public static UserAgreementDialogFragment newInstance(String strCancel, String strSubmit) {

        Bundle args = new Bundle();
        if (UIUtils.isNotEmpty(strCancel)) args.putString("strCancel", strCancel);
        if (UIUtils.isNotEmpty(strSubmit)) args.putString("strSubmit", strSubmit);
        UserAgreementDialogFragment fragment = new UserAgreementDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected UserAgreementDialogFragmentBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.user_agreement_dialog_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments() != null) {
            String strCan = getArguments().getString("strCancel", "取消");
            String strSubmit = getArguments().getString("strSubmit", "同意");

            binding.btnSubmit.setText(strSubmit);
            binding.btnCancel.setText(strCan);
        }


        binding.btnCancel.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (mListener != null) {
                    mListener.onCancelClick();
                }
                dismiss();
            }
        });

        binding.btnSubmit.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                if (mListener != null) {
                    mListener.onSubmitClick();
                }
                dismiss();
            }
        });

        SpanUtils.with(binding.tvBottom)
                .append("您可阅读完整的")
                .append("《用户服务协议》")
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(0xff2892E4);
                        ds.setUnderlineText(false);
                    }

                    @Override
                    public void onClick(@NonNull View widget) {
                        UserAgreementActivity.lunch(getActivity(), true);
                    }
                })
                .append("和")
                .append("《用户隐私协议》")
                .setClickSpan(new ClickableSpan() {
                    @Override
                    public void updateDrawState(@NonNull TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(0xff2892E4);
                        ds.setUnderlineText(false);
                    }

                    @Override
                    public void onClick(@NonNull View widget) {
//                        UserPrivacyActivity.lunch(getActivity());
                        UserAgreementActivity.lunch(getActivity(), false);
                    }
                })
                .append("我们不会向任何第三方提供您的信息，除非得到您的授权。")
                .create();


    }

    @Override
    public void initData() {
        super.initData();


    }

    @Override
    protected boolean isCancel() {
        return false;
    }

    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getWidth() {
        return DensityUtil.getDimens(R.dimen.dp_294);
    }


    private onUserAgreementItemClickListener mListener;


    public void setListener(onUserAgreementItemClickListener listener) {
        this.mListener = listener;
    }

    public interface onUserAgreementItemClickListener {

        void onSubmitClick();

        void onCancelClick();
    }

}
